package com.joehongfa.util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.Random;

import org.jsoup.nodes.Document;

public class SaveToFileUtil {

	// 以html保存
	public static void SaveAsType(Document document, String type, String... args) {
		// System.out.println(document.html());
		String fileName = document.getElementsByTag("title").get(0).text();
		if (args.length != 0) {
			// 不指定文件名,以网页title命名
			fileName = args[0];
		}
		if (fileName == null || fileName.length() == 0) {
			fileName = new Random().nextInt() + "";
		}
		System.out.println(fileName);
		// System.out.println(document.baseUri());
		FileWriter fileWriter = null;
		try {
			fileWriter = new FileWriter(new File("log/" + fileName + "." + type));
			fileWriter.write(document.html());
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileWriter != null)
				try {
					fileWriter.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		System.out.println("success");
		System.out.println("log/" + fileName + "." + type);
	}
}
